﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using dotNetDR_OAuth2;
using dotNetDR_OAuth2.AccessToken;
using dotNetDR_OAuth2.APIs;

namespace dotNetDR_OAuth2.Sample.MVC.Controllers
{
    public class TencentController : Controller
    {
        private IAuthorizationCodeBase _authCode = Uf.C(CtorAT.Tencent);
        private TencentError _err = null;
        private IApi api = Uf.C(CtorApi.Tencent);

        public ActionResult Index(string code, string openid, string openkey)
        {
            if (Session["accessToken"] != null)
            {
                return View(new object());
            }

            if (!string.IsNullOrEmpty(code))
            {
                var redirectUrl = AccessTokenToolkit.GenerateHostPath(Request.Url) + Url.Action("Index");

                var accessToken = _authCode.GetResult(_authCode.GenerateAccessTokenUrl(redirectUrl, code));

                if (api.WasError(accessToken, out _err))
                {
                    Session["err"] = _err;
                    return RedirectToAction("Error");
                }

                accessToken.openid = openid; //注意腾讯微创新
                accessToken.openkey = openkey; //注意腾讯微创新

                Session.Add("accessToken", accessToken);

                return View(new object());
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        [NonAction]
        private IDictionary<string, object> GetOpenidOpenkeyParamsExt()
        {
            var result = new Dictionary<string, object>();

            var accessTokenObj = Session["accessToken"] as dynamic;
            var openid = accessTokenObj.openid;
            var openkey = accessTokenObj.openkey;

            result.Add("openid", openid);
            result.Add("openkey", openkey);

            return result;
        }

        [AccessTokenRequired]
        public ActionResult ShowUserInfo()
        {
            var accessTokenObj = Session["accessToken"] as dynamic;
            var accessToken = accessTokenObj.access_token;

            var model = api.CallGet("user/info?format=json", accessToken, false, GetOpenidOpenkeyParamsExt());

            if (api.WasError(model, out _err))
            {
                Session["err"] = _err;
                return RedirectToAction("Error");
            }

            return View(model.data);
        }

        [AccessTokenRequired]
        public ActionResult PublishMsg()
        {
            var accessTokenObj = Session["accessToken"] as dynamic;
            var uid = accessTokenObj.name;
            var accessToken = accessTokenObj.access_token;
            var openid = accessTokenObj.openid;

            var msg = MsgConfig.GetMsg();
            var formData = new Dictionary<string, string>();
            formData.Add("content", Server.UrlEncode(msg));

            var result = api.CallPost("t/add?format=json", accessToken, formData, false, GetOpenidOpenkeyParamsExt());
            if (api.WasError(result, out _err))
            {
                Session["err"] = _err;
                return RedirectToAction("Error");
            }

            return View();
        }

        public ActionResult Error()
        {
            var err = Session["err"] as TencentError;

            return View(err);
        }
    }
}
